package gherkinedit.editors;

import gherkinedit.utils.FilesystemUtils;
import gherkinedit.utils.StringUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.contentassist.CompletionProposal;
import org.eclipse.jface.text.contentassist.ContextInformation;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.jface.text.contentassist.IContextInformationValidator;

public class GherkinContentAssistProcessor implements IContentAssistProcessor {

	@Override
	public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
		
		File rootDir = FilesystemUtils.getCurrentProjectRootDirectory();
		if (rootDir == null) {
			System.out.println("getCurrentProjectRootDirectory() returned null!");
		} else {
			System.out.println("getCurrentProjectRootDirectory() returned: " + rootDir.getAbsolutePath());
		}
		
		List<String> suggestions = new ArrayList<String>();
		List<CompletionProposal> completionProposals = new ArrayList<CompletionProposal>();
		
		String selectedLineBeforeCursor = getSelectedLineBeforeCursor(viewer.getDocument(), offset);
		
		GherkinSentenceCrawler crawler = new GherkinSentenceCrawler();
		List<String> gherkinSentences = crawler.getGherkinSentences();
		
		List<String> matchedSentences = getMatchedSentences(selectedLineBeforeCursor, gherkinSentences);
		
		for (int i=0; i<matchedSentences.size(); i++) {
			
			matchedSentences.set(i, matchedSentences.get(i).substring(selectedLineBeforeCursor.length()));
		}
		
		if (matchedSentences.size() < 1) {
			matchedSentences = crawler.getGherkinSentences();
			matchedSentences = StringUtils.sortStrByAscDistToStr(matchedSentences, selectedLineBeforeCursor);
		}
		
		suggestions.addAll(matchedSentences);
		
		// suggestions.add("Given ");
		// suggestions.add("When ");
		// suggestions.add("Then ");
		// suggestions.add("And ");
		// suggestions.add("But ");
		// suggestions.add("Scenario: ");
		// suggestions.add("Feature: ");

		for (String suggestion : suggestions) {
			completionProposals.add(new CompletionProposal(suggestion, offset, 0, suggestion.length(), null, suggestion,
					(IContextInformation) new ContextInformation("contextDisplayString", "informationDisplayString"), "Additional proposal info"));
		}
		return (ICompletionProposal[]) completionProposals.toArray(new ICompletionProposal[completionProposals.size()]);
	}


	private String getSelectedLineBeforeCursor(IDocument doc, int offset) {
		int start = offset;
		int end = offset;
		try {
			while (start > 0 && doc.getChar(start) != '\n') {
				start--;
				System.out.println("Get char at " + start);
			}
			// while (end < doc.getLength() && doc.getChar(end) != '\n') end++;
			return doc.get(start+1, end-start-1);
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
		return "";
	}

	private List<String> getMatchedSentences(String selectedLineBeforeCursor, List<String> gherkinSentences) {
		List<String> matchedSentences = new ArrayList<String>();
		for (String sentence : gherkinSentences) {
			if (sentence.startsWith(selectedLineBeforeCursor.trim()))
				matchedSentences.add(sentence);
		}
		return matchedSentences;
	}

	@Override
	public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public char[] getCompletionProposalAutoActivationCharacters() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public char[] getContextInformationAutoActivationCharacters() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getErrorMessage() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public IContextInformationValidator getContextInformationValidator() {
		// TODO Auto-generated method stub
		return null;
	}

}
